package com.gaoxin.api.service.statisservice;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gaoxin.api.dao.statismapper.StatisClassNumberMapper;
import com.gaoxin.api.entity.statisentity.Classroom;
import com.gaoxin.api.entity.statisentity.User;
import com.google.common.collect.Lists;

/**
 * 统计用户所带班级信息Service
 * @author zhaozhenshe
 *2018-07-13
 */
@Service
public class StatisClassNumberService {
	
	@Autowired
	private StatisClassNumberMapper statisClassNumberMapper;
	/**
	 * 
	 * @param userCode
	 * @return
	 */
	public User queryClassNumber(String userCode)throws Exception{
		User user = findClassInfo(userCode);
		if(user!=null){
			if(user.getClassNumber()>1){//带了多个班
				String [] classId = user.getClassIdStr().split(",");
				List<Classroom> classList = Lists.newArrayList();
				int sum = 0;//在校总人数
				int sumBoy = 0;//男生总人数
				int sumGirl = 0;//女生总人数
				int sumStu = 0;//所有人数
				for (String string : classId) {
					Classroom classroom = findClassInfoByClassId(string);
					Classroom classroom2 = statisClassStuNumber(string);//班级总人数，男女生人数
					Classroom classroom3 = findClassEnrollments(string);//班级在校人数
					classroom.setBoyNumber(classroom2.getBoyNumber());
					classroom.setGirlNumber(classroom2.getGirlNumber());
					classroom.setStudentNumber(classroom2.getStudentNumber());
					classroom.setEnrollments(classroom3.getEnrollments());
					classroom.setStudentNumber(classroom2.getStudentNumber());
					classList.add(classroom);
					sum += classroom3.getEnrollments();
					sumBoy += classroom2.getBoyNumber();
					sumGirl += classroom2.getGirlNumber();
					sumStu += classroom2.getStudentNumber();
				}
				user.setBoyTotal(sumBoy);
				user.setEnrollmentsTotal(sum);
				user.setGirlTotal(sumGirl);
				user.setClassIdStr(null);
				user.setStudentTotal(sumStu);
				user.setArrayList(classList);
			}else if(user.getClassNumber()>0){//只带了一个班
				List<Classroom> classList = Lists.newArrayList();
				Classroom classroom = findClassInfoByClassId(user.getClassIdStr());//班级基本信息
				Classroom classroom2 = statisClassStuNumber(user.getClassIdStr());//班级总人数，男女生人数
				Classroom classroom3 = findClassEnrollments(user.getClassIdStr());//班级在校人数
				classroom.setBoyNumber(classroom2.getBoyNumber());
				classroom.setGirlNumber(classroom2.getGirlNumber());
				classroom.setStudentNumber(classroom2.getStudentNumber());
				classroom.setEnrollments(classroom3.getEnrollments());
				classroom.setStudentNumber(classroom2.getStudentNumber());
				classList.add(classroom);
				user.setBoyTotal(classroom2.getBoyNumber());
				user.setGirlTotal(classroom2.getGirlNumber());
				user.setEnrollmentsTotal(classroom3.getEnrollments());
				user.setStudentTotal(classroom2.getStudentNumber());
				user.setArrayList(classList);
				user.setClassIdStr(null);
			}
		}
		return user;
	}
	/**
	 * 根据用户员工号查询用户所带班级信息
	 * @param userCode
	 * @return
	 * @throws Exception
	 */
	public User findClassInfo(String userCode)throws Exception{
		return statisClassNumberMapper.findClassInfo(userCode);
	}
	/**
	 * 根据班级Id查询在校人数
	 * @param classId
	 * @return
	 */
	public Classroom findClassEnrollments(String classId)throws Exception{
		return statisClassNumberMapper.findClassEnrollments(classId);
	}
	/**
	 * 根据班级Id查询班级具体信息
	 * @param classId
	 * @return
	 */
	public Classroom findClassInfoByClassId(String classId)throws Exception{
		return statisClassNumberMapper.findClassInfoByClassId(classId);
	}
	/**
	 * 统计班级总人数，男女生人数
	 * @param ClassId
	 * @return
	 */
	public Classroom statisClassStuNumber(String ClassId){
		return statisClassNumberMapper.statisClassStuNumber(ClassId);
	}
	/**
	 * 查询该用户是否存在
	 * @param userCode
	 * @return
	 */
	public User queryUser(String userCode){
		return statisClassNumberMapper.queryUser(userCode);
	}
}
